package com.netnovelstore.controller;

import com.netnovelstore.annotation.ResultInterceptor;
import com.netnovelstore.pojo.Exception.NotDataException;
import com.netnovelstore.pojo.Result;
import com.netnovelstore.pojo.User;
import com.netnovelstore.service.UserService;
import com.netnovelstore.viewObject.UserListResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@ResultInterceptor
@RestController
@RequestMapping("/Manage/User")
@CrossOrigin
@Slf4j
@Api(tags = "用户管理接口")
public class ManageUserController {

    private UserService userServiceImpl;

    @Autowired
    public ManageUserController(UserService userServiceImpl) {
        this.userServiceImpl = userServiceImpl;
    }


    @DeleteMapping("/delUserByNumberList")
    @ApiOperation("用户编号删除用户")
    public Result delUserByNumberList(@RequestBody @NotEmpty List<Integer> numberList) throws Exception {
        log.info(numberList.toString());
        return userServiceImpl.delUserByNumberList(numberList);
    }

    @GetMapping("/getUserList")
    @ApiOperation("用于用户表格分页")
    public Result<UserListResult> getUserList(@NotNull Integer currentPage, @NotNull Integer pageSize) throws Exception {
        return userServiceImpl.getUserList(currentPage, pageSize);
    }

    @GetMapping("/searchUserByInfo")
    @ApiOperation("用于用户模糊搜索")
    public Result<List<User>> searchUserByInfo(String keyword) throws Exception {
        return userServiceImpl.searchUserByInfo(keyword);
    }

    @PatchMapping("/updateUser")
    @ApiOperation("更新信息用户信息")
    public Result updateUser(@RequestBody User user, HttpServletResponse response) throws IOException, SQLException, NotDataException {
        log.info(user.toString());
        log.info(user.getUserName());
        return userServiceImpl.updateUserByNumber(user);
    }
}
